package com.wsj.a.collapsingdemo.ui;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.wsj.a.collapsingdemo.R;
import com.wsj.a.collapsingdemo.adapter.WaterFallAdapter;
import com.wsj.a.collapsingdemo.callback.OnRcvScrollListener;
import com.wsj.a.collapsingdemo.callback.StateListener;
import com.wsj.a.collapsingdemo.databinding.ActivityTestRefreshLoadMoreBinding;
import com.wsj.a.collapsingdemo.entity.WorkEntity;
import com.wsj.a.collapsingdemo.utils.Logger;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class TestRefreshLoadMoreActivity extends AppCompatActivity implements StateListener{

    private ActivityTestRefreshLoadMoreBinding mBinding;
    private WaterFallAdapter mAdapter;
    private List<WorkEntity> userDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(TestRefreshLoadMoreActivity.this,R.layout.activity_test_refresh_load_more);
        initData();
        initRecyclerView();
        initView();
    }

    public void initView(){

        mBinding.ptrFrameLayout.setLastUpdateTimeRelateObject(this);
        mBinding.ptrFrameLayout.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                 onLoadMore();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                onRefresh();
            }

            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                return super.checkCanDoLoadMore(frame, mBinding.recyclerView, footer);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return super.checkCanDoRefresh(frame, mBinding.recyclerView, header);
            }
        });
        // the following are default settings
        mBinding.ptrFrameLayout.setResistance(1.7f); // you can also set foot and header separately
        mBinding.ptrFrameLayout.setRatioOfHeaderHeightToRefresh(1.2f);
        mBinding.ptrFrameLayout.setDurationToClose(300);  // you can also set foot and header separately
        // default is false
        mBinding.ptrFrameLayout.setPullToRefresh(false);

        // default is true
        mBinding.ptrFrameLayout.setKeepHeaderWhenRefresh(true);
        mBinding.ptrFrameLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mBinding.ptrFrameLayout.autoRefresh();
            }
        }, 100);
    }

    public List<WorkEntity> initData(){
        userDatas = new ArrayList<>();
        String [] nicknames = new String[]{"范冰冰","赵薇","袁泉","赵丽颖","李冰冰","舒淇","黄圣依","刘亦菲","曹颖","杨幂"};
        String [] headImages = new String[]{"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788329728&di=051c7504b400b838376aa7eec2a66bb5&imgtype=0&src=http%3A%2F%2Fs15.sinaimg.cn%2Fmw690%2F001PrNfEgy71DVOVBgife%26690",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788395118&di=84a61ec3df0f80878f22cd24c02d1f92&imgtype=0&src=http%3A%2F%2Fp.cdn.sohu.com%2Fbc4e413e%2Fe1e4b530152dc4cdd6ec340dffaac618.jpeg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788448601&di=d55977be14613dda0d07a510243368f4&imgtype=0&src=http%3A%2F%2Fapp.why.com.cn%2Fepaper%2Fshzk%2Fimages%2F2014-06%2F24%2FA19%2Fa19p19_b.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788495763&di=a634f95181f7c1be9cf09937f3c64080&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201404%2F28%2F20140428222525_heZZX.jpeg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788530823&di=def5c2fb972a5c32d948758a96eea2e9&imgtype=0&src=http%3A%2F%2Fimg3.cache.netease.com%2Fphoto%2F0026%2F2014-08-18%2FA3UNS1N525A20026.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788556296&di=01fda63499bb9483406a90aaf473ce5d&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fitem%2F201502%2F06%2F20150206084438_xYejE.thumb.700_0.jpeg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788615036&di=604eaea7865e2041bc319a876b385096&imgtype=0&src=http%3A%2F%2Fimg01.jixiexinxi5.com%2Fupload%2F201701%2F22%2F135905901.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788644700&di=64ce46b5a786ce16a66757347c14a101&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201501%2F09%2F20150109093459_KmVrx.jpeg",
                "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1100322184,3942798567&fm=11&gp=0.jpg",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533788714713&di=c9ecf2fc30e490bb62d0bb20d4f1dd64&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201510%2F08%2F20151008112536_GVRwJ.jpeg"};

        for(int i=0;i<10;i++) {
            WorkEntity workEntity = new WorkEntity();
            workEntity.headImage = headImages[i];
            workEntity.nickname = nicknames[i];
            workEntity.imageHeight = (i % 2) * 100 + 400; //偶数和奇数的图片设置不同的高度，以到达错开的目的
            userDatas.add(workEntity);
        }
        return userDatas;
    }

    public void initRecyclerView(){
        StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mAdapter = new WaterFallAdapter(TestRefreshLoadMoreActivity.this,null);

        mBinding.recyclerView.setLayoutManager(mLayoutManager);
        mBinding.recyclerView.setAdapter(mAdapter);
        mBinding.recyclerView.addOnScrollListener(new OnRcvScrollListener(){
            @Override
            public void onBottom() {
                super.onBottom();
                Logger.d("isRefreshing-->"+mBinding.ptrFrameLayout.isRefreshing());
                if(!mBinding.ptrFrameLayout.isRefreshing()){
                    onLoadMore();
                }
            }
        });
    }


    @Override
    public void onRefresh() {
        mBinding.ptrFrameLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.getDataList().clear();
                mAdapter.getDataList().addAll(initData());
                mBinding.ptrFrameLayout.refreshComplete();
                mAdapter.notifyDataSetChanged();
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        mBinding.ptrFrameLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.getDataList().addAll(initData());
                mBinding.ptrFrameLayout.refreshComplete();
                mAdapter.notifyDataSetChanged();
            }
        }, 1000);
    }

}
